#include "staticmain.h"

StaticMain::StaticMain()
{
}

void StaticMain::LoadPhysicists()
{
    this->registeredphysicists.clear();
    QDomDocument doc("mydocument");
    QFile file("db/physicists.xml");
    if (!file.open(QIODevice::ReadOnly))
    {
        return;
    }
    if (!doc.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();
    // print out the element names of all elements that are direct children
    // of the outermost element.
    QDomElement docElem = doc.documentElement();

    QDomNode currentNode = docElem.firstChild();
    while(!currentNode.isNull())
    {
        QDomElement e = currentNode.toElement(); // try to convert the node to an element.
        if(!e.isNull())
        {
            physicist* currentphys = new physicist(e.childNodes());
            this->registeredphysicists.append(*currentphys);
        }
        currentNode = currentNode.nextSibling();
    }
}

void StaticMain::AddPhysicists(QString prename, QString lastname, QString team)
{
    QDomDocument doc("mydocument");
    QFile file("db/physicists.xml");
    if (!file.open(QIODevice::ReadOnly))
    {
        debugger::show("File can not be opened.");
        return;
    }
    if (!doc.setContent(&file))
    {
        debugger::show("File is corrupted.");
        return;
    }
    file.close();
    // print out the element names of all elements that are direct children
    // of the outermost element.
    QDomElement docElem = doc.documentElement();

    //QDomNode currentNode = docElem.firstChild();
    QDomNode currentNode = docElem.lastChild();
    QDomElement e = currentNode.toElement(); // try to convert the node to an element.
    int maxid = 1;
    if(!e.isNull())
        maxid = e.childNodes().at(0).toElement().text().toInt();

    QDomElement newPhysicistTag = doc.createElement(QString("physicist"));

    QDomElement newIDTag = doc.createElement(QString("id"));
    QDomText newIDText = doc.createTextNode(QString::number(maxid+1));
    newIDTag.appendChild(newIDText);

    QDomElement newPreNameTag = doc.createElement(QString("prename"));
    QDomText newPreNameText = doc.createTextNode(prename);
    newPreNameTag.appendChild(newPreNameText);

    QDomElement newLastNameTag = doc.createElement(QString("surname"));
    QDomText newLastNameText = doc.createTextNode(lastname);
    newLastNameTag.appendChild(newLastNameText);

    QDomElement newTeamTag = doc.createElement(QString("team"));
    QDomText newTeamText = doc.createTextNode(lastname);
    newTeamTag.appendChild(newTeamText);

    debugger::show(newLastNameText.data());

    newPhysicistTag.appendChild(newIDTag);
    newPhysicistTag.appendChild(newPreNameTag);
    newPhysicistTag.appendChild(newLastNameTag);
    newPhysicistTag.appendChild(newTeamTag);

    docElem.appendChild(newPhysicistTag);

    QString xmlIn = doc.toString();

    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
    {
        debugger::show("File can not be opened.");
        return;
    }

    QXmlStreamReader reader(xmlIn);
    QXmlStreamWriter writer(&file);
    writer.setAutoFormatting(true);
    writer.setCodec("ISO-8859-1");

    writer.writeStartDocument("1.0",true);
    writer.writeDTD(//"<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\n"
                    "<!DOCTYPE physicists [\n"
                    "<!ELEMENT physicists (physicist)*>\n"
                    "<!ELEMENT physicist (id, prename, surname, team)>\n"
                    "<!ELEMENT id (#PCDATA)>\n"
                    "<!ELEMENT prename (#PCDATA)>\n"
                    "<!ELEMENT surname (#PCDATA)>\n"
                    "<!ELEMENT team (#PCDATA)>\n"
                    "]>\n");

    if(!reader.atEnd())
        reader.readNext();
    if(!reader.atEnd())
        reader.readNext();

    while (!reader.atEnd())
    {
        reader.readNext();
        if (!reader.isWhitespace())
        {
            writer.writeCurrentToken(reader);
        }
    }
    file.close();
    this->LoadPhysicists();
}

void StaticMain::LoadSamples()
{
    QDomDocument doc("mydocument");
    QFile file("db/samples.xml");
    if (!file.open(QIODevice::ReadOnly))
    {
        return;
    }
    if (!doc.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();
    // print out the element names of all elements that are direct children
    // of the outermost element.
    QDomElement docElem = doc.documentElement();

    QDomNode currentNode = docElem.firstChild();
    while(!currentNode.isNull())
    {
        QDomElement e = currentNode.toElement(); // try to convert the node to an element.
        if(!e.isNull())
        {
            sample* currentsample = new sample(e.childNodes());
            this->registeredsamples.append(*currentsample);
        }
        currentNode = currentNode.nextSibling();
    }
}

void StaticMain::LoadTemplates()
{
    LoadPhysicists();
    LoadSamples();
}

QList<physicist> StaticMain::GetRegPhysList()
{
    return this->registeredphysicists;
}

physicist StaticMain::GetRegPhys(int id)
{
    foreach (physicist phys, this->registeredphysicists)
        if (phys.GetID() == id)
            return phys;
}


QList<sample> StaticMain::GetRegSamples()
{
    return this->registeredsamples;
}
